<?php


namespace app\job;
use app\service\propertyService;
use app\service\RedisQueueService;
use app\service\zftService;
use app\zftpay\service\apiService;
use think\facade\Db;
use think\facade\Queue;
use think\queue\Job;
use think\queue\ShouldQueue;

//账单结算
class ZftSettleOrder implements ShouldQueue
{
    public $jobData;

    public function __construct($jobData)
    {
        $this->jobData = $jobData;
    }

    /**
     * fire方法是消息队列默认调用的方法
     * @param Job            $job      当前的任务对象
     * @param array|mixed    $data     发布任务时自定义的数据
     */
    public function fire(Job $job,$data){
        // 如有必要,可以根据业务需求和数据库中的最新数据,判断该任务是否仍有必要执行.
        //执行收单交易结算

        $isJobDone = $this->doJob($this->jobData);
        print("<info>BillpaySettle Job Started. job Data is: ".var_export($isJobDone,true)."</info> \n");

        if ($isJobDone) {

            $job->delete();
        }else{
            if ($job->attempts() > 3) {
                //记录开票推送失败的记录
                $job->delete();

                (new RedisQueueService())->push("ZftSettleOrderFailed",$data["id"]);

            }
        }
    }

    public function doJob($data){
        $info=Db::table("zft_order_success")->where("uid",$data["id"])->find();
        if(empty($info)){
            return (new RedisQueueService())->push("ZftSettleOrderFailed",$data["id"]);
        }
        if($info["settle_status"]==1){
            return true;
        }
        //查询该账单是否已经结算
        $propertyZftInfo=(new propertyService())->propertyZftByComId($info["community_id"]);
        if(empty($propertyZftInfo)){
            print("<info>Hello Job Started. job Data is: 直付通配置为空</info> \n");
            return json(['info' => '直付通配置为空']);
        }
        $api=new zftService();
        $amount=round($propertyZftInfo["taxrate"]*$info["order_money"],2);
        if($amount<=0){
            Db::table("zft_order_success")->where("id",$info["uid"])->update([
                "settle_status"=>1
            ]);
            $settle["community_id"]=$info["community_id"];
            $settle["trade_no"]=$info["trade_no"];
            $settle["order_uid"]=$info["uid"];
            $settle["order_money"]=$info["pay_money"];
            $settle["settle_money"]=$amount;
            $settle["community_name"]=$info["community_name"];
            $settle["settle_no"]="";
            $settle["status"]="1";
            $settle["createTime"]=time();
            Db::table("zft_settle_order")->insert($settle);
            return true;
        }

        $royalty_parameters=array(
            'royalty_type'=>'transfer',//'loginName',
            'trans_in_type'=>'loginName',//收入方账户类型
            'trans_in'=>'aliwykd@163.com',//收入方账户\linan2421@163.com
            'amount'=>$amount,//分账金额
        );
        $data= array(
            'out_trade_no'=> $info["trade_no"],
            'trade_no'=>$info["trade_no"],
            'royalty_parameters'=>$royalty_parameters,
        );

        $result = $api->settle(json_encode($data));
        $add_log["result_code"]=$result->code;
        $add_log["order_uid"]=$info["uid"];
        $add_log["create_time"]=date("Y-m-d H:i:s");
        $add_log["source"]="queue";
        if($result->code==10000){
            $settle["community_id"]=$info["community_id"];
            $settle["trade_no"]=$info["trade_no"];
            $settle["order_uid"]=$info["uid"];
            $settle["order_money"]=$info["pay_money"];
            $settle["settle_money"]=$amount;
            $settle["community_name"]=$info["community_name"];
            $settle["settle_no"]=$result->settle_no;
            $settle["createTime"]=time();
            $settle["ctime"]=date("Y-m-d H:i:s");
            $resultId=Db::table("zft_settle_order")->insertGetId($settle);
            $add_log["out_request_no"]=$data["out_trade_no"];
            $add_log["trade_no"]=$data["trade_no"];
            $add_log["settle_no"]=$settle["settle_no"];
            Db::table("zft_log_settle_order")->insert($add_log);

            $jobQueueName  	  = "app\job\ZftSettleQueryOrder";
            $jobData       	  = [ 'id' => $resultId ] ;
            $res=Queue::push($jobQueueName,$jobData);
        }else{
            $add_log["result_msg"]=$result->sub_msg;
            $add_log["sub_code"]=$result->sub_code;
            $add_log["out_request_no"]=$info["out_trade_no"];
            $add_log["trade_no"]=$info["trade_no"];
            $logId=Db::table("zft_log_settle_order")->insertGetId($add_log);
            $settle["community_id"]=$info["community_id"];
            $settle["trade_no"]=$info["trade_no"];
            $settle["order_uid"]=$info["uid"];
            $settle["order_money"]=$info["pay_money"];
            $settle["settle_money"]=$amount;
            $settle["community_name"]=$info["community_name"];
            $settle["createTime"]=time();
            $settle["ctime"]=date("Y-m-d H:i:s");
            $settle["fail_msg"]=$add_log["result_msg"];
            $settle["log_settle_order_id"]=$logId;
            $res=Db::table("zft_settle_order_fail")->insert($settle);

        }
        print("<info>Hello Job Started. job Data is: ".var_export($result,true)."</info> \n");


        return true;
    }
}